package com.sumainfor.wx.system.epc.service;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.wx.system.epc.dao.ReceiveMapper;
import com.sumainfor.wx.system.feign.ZuulFeign.ZuulFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 *
 * @Auther: zhlu
 * @Date: 2019/6/12 18:48
 * @Description:
 */
@Service
public class ReceiveService {

    @Autowired
    ReceiveMapper ReceiveMapper;

    @Autowired
    ZuulFeign ZuulFeign;

    @Autowired
    ComUtils ComUtils;

    /**
     * 功能描述: 
     * 获取当前用户的领用单列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/12 18:49
     */
    public List<Map<String,Object>> getReceiveList(Map<String,Object>params){
        params.put("SysUserId", ContextUtils.getUserId());
        List<Map<String,Object>>getReceiveList=ReceiveMapper.getReceiveList(params);
        if(getReceiveList.size()>0){
            for(Map<String,Object> map:getReceiveList){
                List<String>ids=new ArrayList<String>();
                ids.add(map.get("SysUserId").toString());
                JsonResult jsonResult=ZuulFeign.getUser(ids);
                List<Map<String,Object>>UserList=(List<Map<String,Object>>)jsonResult.getResult();
                map.put("username",UserList.get(0).get("username"));
                map.put("deptName",UserList.get(0).get("deptName"));
//                map.putAll(UserList.get(0));
            }
        }
        return getReceiveList;
    }

    /**
     * 功能描述: 
     * 获取当前用户的领用单列表总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/12 18:49
     */
    public Integer getReceiveListCount(Map<String,Object>params){
        params.put("SysUserId", ContextUtils.getUserId());
        return ReceiveMapper.getReceiveListCount(params);
    }

    /**
     * 功能描述: 
     * 根据领用单编号获取领用备件信息详情
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/12 20:04
     */
    public Map<String,Object>getReceiveById(Map<String,Object>params){
        Map<String,Object>getReceiveById=ReceiveMapper.getReceiveById(params);
        List<Map<String,Object>>getReceiveInfoById=ReceiveMapper.getReceiveInfoById(getReceiveById);
        if(getReceiveInfoById.size()>0){
            getReceiveById.put("ReceiveInfo",getReceiveInfoById);
        }else {
            getReceiveById.put("ReceiveInfo",new ArrayList<Map<String,Object>>());
        }
        List<String>ids=new ArrayList<String>();
        ids.add(getReceiveById.get("SysUserId").toString());
        JsonResult jsonResult=ZuulFeign.getUser(ids);
        List<Map<String,Object>>UserList=(List<Map<String,Object>>)jsonResult.getResult();
        if(UserList.size()>0){
            getReceiveById.put("username",UserList.get(0).get("username"));
            getReceiveById.put("deptName",UserList.get(0).get("deptName"));
        }else{
            getReceiveById.put("username",null);
            getReceiveById.put("deptName",null);
        }
        return getReceiveById;
    }

    /**
     * 功能描述: 
     * 申请领用单主表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/13 10:34
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean AddReceive(Map<String,Object>params){
        boolean result=false;
        //生成UUID
        String RecId = ComUtils.getLocalTrmSeqNumByLook("RE", Constants.DATA_SOURCE_TCEPC, "tcepc_receive", "RecId");
        try {
            //所有新增处理
            Map<String,Object>commonInsertParms=new HashMap<String,Object>();
            commonInsertParms.put("by", ContextUtils.getUserId());//操作人
            commonInsertParms.put("RecId",RecId);//领用单编号
            commonInsertParms.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
            commonInsertParms.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(commonInsertParms);

            params.putAll(commonInsertParms);
            params.put("SysUserId",ContextUtils.getUserId());//领用人
            //没有来源编号    领用单类型  1.维修工单   2申请备件
            if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"SourceId"})){
                params.put("RecType",2);
            }else{
                params.put("RecType",1);
            }

            //生成领用单主表
            ReceiveMapper.AddReceive(params);
            //领用单详表
            List<Map<String, Object>> receiveList = (List<Map<String, Object>>) params.get("receiveList");
            if(receiveList.size()>0){
                for(Map<String, Object> map:receiveList){
                    map.putAll(commonInsertParms);
                    //生成领用单详表
                    ReceiveMapper.AddReceiveInfo(map);
                }
            }
            result=true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

}

